package com.ossean;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.ossean.dao.DBDest;
import com.ossean.dao.DBSource;
import com.ossean.dao.TransferPrjDao;
import com.ossean.model.EddRelations;
import com.ossean.util.TransferProjectsUtil;
import com.ossean.util.TransferProjectsUtil2;


@Component("multiThreadTransferProjects")
@Scope("prototype")
public class MultiThreadTransferProjects implements Runnable{
	Logger logger = Logger.getLogger(this.getClass());
	
	@Resource
	private DBSource dbSource;
	@Resource
	private DBDest dbDest;
	@Resource
	private TransferPrjDao transferPrjDao;
	@Qualifier("transferProjectsUtil2")
	@Autowired
	private TransferProjectsUtil2 transferProjectsUtil2;
	
	
	private String pointerTableName =  TableName.pointerTableName;
	private String sourceTableName = TableName.eddRelationTableName;
	private String targetTableName = TableName.openSourceProjectsTableName;
	private String taggingTableName = TableName.taggingsTableName;
	
	private int batchSize = 500;
	private int startId;
	private int endId;
	private String threadName;
	public void setParameters(int startId,int endId,String threadName) {
		this.startId = startId;
		this.endId = endId;
		this.threadName = threadName;
	}

	
	@Override
	public void run(){
		
    	logger.info(threadName+" is begin transfer projects~");
		Thread.currentThread().setName(threadName);
		int beginId = startId;
		while(beginId < endId){
			List<EddRelations> eddRelationList = transferPrjDao.getEddRelationListInterval(sourceTableName, beginId,endId, batchSize);
			for(EddRelations relation:eddRelationList){
				boolean isUpdate = false;
				String gather_projects_ids = relation.getGather_projects_ids();
				try {
					gather_projects_ids = gather_projects_ids.substring(1, gather_projects_ids.length() - 1);
				} catch (Exception e1) {
					System.out.println(relation.getGather_projects_ids());
					e1.printStackTrace();
				}
				String[] idsArray = gather_projects_ids.split(",");
				for(int i = 0; i < idsArray.length; i++){
					int id = Integer.parseInt(idsArray[i]);
					if(null != dbDest.selectOpenSourceProjectsItem(targetTableName,id)){
						isUpdate = true;
						break;
					}
				}
				int prjId = transferProjectsUtil2.handleOneRelation(relation,isUpdate);
				//System.out.println(prjId);
			}
			beginId = eddRelationList.get(eddRelationList.size()-1).getId();
		}
		TransferManageProcess.gatherState.put(threadName, false);
		logger.info(threadName+" is stop");
	}
	
	public static String getTargetTable(int ospId){
		String targetTableName = "";
		if(ospId >= 770000){
			targetTableName = "relative_memo_to_open_source_projects_70";
		}
		else{
			int a = 1 + ospId/11000;
			targetTableName = "relative_memo_to_open_source_projects_" + a;
		}

		return targetTableName;
	}


	

}
